<template>
  <div class="p-d">
    <div v-if="userAuth == 1287">
      <facilitiesCopy />
    </div>
    <div class="b-f p-d" v-else>
      <el-tabs v-model="queryString.activeName" @tab-click="handleClick">
        <el-tab-pane label="全部" name="all"></el-tab-pane>
        <el-tab-pane label="共享资源目录" name="sharing"></el-tab-pane>
        <el-tab-pane label="不共享资源目录" name="noShare"></el-tab-pane>
      </el-tabs>
      <div class="p-t">
        <SearchComp :searchForm="searchForm" :searchData="queryString" :searchAction="searchAction"></SearchComp>
      </div>
      <div class="p-t">
        <div class="flex-between">
          <div class="btnStyle flex-a-c ">
            <!-- <ButtonComp :buttonData="buttonData" /> -->
            <el-button class="m-l-5 m-r-5" size="small" type="primary" @click="addFacilitie">新增</el-button>
            <el-button class="m-l-5 m-r-5" size="small" type="warning" @click="handleDownloadFile">一键导出</el-button>
            <el-upload class="m-l-5 m-r-5" action="#" :on-change="handleUploadFile" :show-file-list="false"
              :auto-upload="false">
              <el-button size="small" type="warning">一键导入</el-button>
            </el-upload>
            <el-button class="m-l-5 m-r-5" size="small" type="danger" @click="batchDelete">批量删除</el-button>
            <!-- <el-button size="small" type="warning">一键导入</el-button> -->
          </div>
        </div>
        <TableCompEle class="m-t" ref="multipleTable" :tableData="tableData" :tableConfig="tableConfig"
          :tableAction="tableAction" :isSelect="true" :isPageIndex="true" :isShowPage="true" :queryParams="queryParams"
          @getPagination="getList" @handleSelectionChange="handleSelectionChange">
        </TableCompEle>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getFacilitiesList,
  updateFacilities,
  deleteFacilitiesBatch,
  downloadMultiple,
  uploadMultiple
} from '@/api/yqgx/facilities'
import facilitiesCopy from './facilitiesCopy.vue'
export default {
  components: { facilitiesCopy },
  data() {
    return {
      userAuth: localStorage.getItem('userInfo')
        ? JSON.parse(localStorage.getItem('userInfo'))[0].userId
        : 1287,
      activeName: 'all',
      multipleSelection: [],
      queryParams: {
        total: 20,
        currentPage: 1,
        pageSize: 10,
        type: ''
      },
      buttonData: [
        {
          kind: 'button',
          label: '新增',
          plain: false,
          size: 'small',
          type: 'primary',
          click: () => {
            this.$router.push({
              name: 'facilitiesInfo',
              params: { flag: 'add' }
            })
          }
        },
        {
          kind: 'button',
          label: '一键导出',
          plain: false,
          size: 'small',
          type: 'warning',
          click: () => {
            if (this.multipleSelection.length == 0) {
              this.$message({
                type: 'warning',
                message: '请选择要导出的数据'
              })
              return
            }
            const ids = []
            this.multipleSelection.forEach(items => {
              ids.push(items.id)
            })
            this.downloadFun(ids)
          }
        },
        // {
        //   kind: 'button',
        //   label: '一键导入',
        //   plain: false,
        //   size: 'small',
        //   type: 'warning',
        //   click: () => {

        //   }
        // },
        {
          kind: 'button',
          label: '批量删除',
          plain: false,
          size: 'small',
          type: 'danger',
          click: () => {
            if (this.multipleSelection.length == 0) {
              this.$message({
                type: 'warning',
                message: '请选择要删除的数据'
              })
              return
            }
            const ids = []
            this.multipleSelection.forEach(items => {
              ids.push(items.id)
            })
            this.deleteFun(ids)
          }
        }
      ],
      tableData: [],
      allData: [],
      tableConfig: [
        {
          prop: 'facilityNum',
          label: '资产编号'
        },
        {
          prop: 'facilityNameCn',
          label: '设施名称',
          isLink: true,
          type: 'primary',
          method: () => {
            this.addFacilitie()
          }
        },
        {
          prop: 'type',
          label: '设施类别'
        },
        {
          prop: 'managementUnit',
          label: '管理单位'
        },
        {
          prop: 'yearStr',
          label: '启用日期'
        },
        {
          prop: 'investmentScale',
          label: '投资规模(万元)'
        },
        {
          prop: 'sharedScope',
          label: '共享范围'
        },
        {
          prop: 'ReportTime',
          label: '上报时间'
        },
        {
          label: '状态',
          prop: 'Report',
          width: '150px',
          isStatus: row => {
            switch (row.Report) {
              case '待审核': // 待上报
                return { color: 'blue', bgColor: 'blue-bg' }
              case '待上报': // 待上报
                return { color: 'blue', bgColor: 'blue-bg' }
              case '已上报': // 待审核
                return { color: 'green', bgColor: 'green-bg' }
              default: // 审核通过已上报
                return { color: 'red', bgColor: 'red-bg' }
            }
          }
        }
      ],
      tableAction: {
        align: () => 'center',
        width: () => String(190),
        operations: [
          {
            label: '查看',
            isLink: true,
            type: 'primary',
            method: row => {
              this.$router.push({
                name: 'facilitiesInfo',
                params: { flag: 'look', id: row.id }
              })
              // this.$router.push({
              //   name: 'kyssDetail',
              //   params: { flag: 'look', id: row.id }
              // })
            }
          },
          {
            label: '上报',
            isLink: true,
            type: 'primary',
            isShow: (row) => {
              return row.Report == '待上报'
            },
            method: row => {
              this.handleReport(row, '已上报')
            }
          },
          {
            label: '审核',
            isLink: true,
            type: 'primary',
            isShow: (row) => {
              return row.Report == '待审核'
            },
            method: row => {
              this.$router.push({
                name: 'facilitiesInfo',
                params: { flag: 'look', isAudit: true, id: row.id }
              })
            }
          },
          {
            label: '修改',
            isLink: true,
            type: 'primary',
            isShow: (row) => {
              return (row.Report == '待审核')
            },
            method: row => {
              this.$router.push({
                name: 'facilitiesInfo',
                params: { flag: 'edit', id: row.id }
              })
              // this.$router.push({
              //   name: 'kyssDetail',
              //   params: { flag: 'edit', id: row.id }
              // })
            }
          },
          {
            label: '下载',
            isLink: true,
            type: 'primary',
            method: row => {
              this.downloadFun([row.id])
            }
          },
          {
            label: '删除',
            isLink: true,
            type: 'danger',
            isShow: (row) => {
              return (row.Report == '待审核')
            },
            method: row => {
              this.deleteFun([row.id])
            }
          }
        ]
      },
      queryString: {
        facilityNameCn: '',
        type: '',
        managementUnit: '',
        activeName: 'all'
      },
      searchForm: {
        changeMethod: () => {
          this.queryParams.currentPage = 1
        },
        formData: [
          {
            type: 'Input',
            label: '科研设施名称',
            prop: 'facilityNameCn',
            clearable: true,
            icon: 'el-icon-search',
            placeholder: '请输入'
          },
          {
            type: 'Input',
            label: '科研设施类型',
            prop: 'type',
            clearable: true,
            icon: 'el-icon-search',
            placeholder: '请输入'
          },
          {
            type: 'Input',
            label: '所属大单位',
            prop: 'managementUnit',
            clearable: true,
            icon: 'el-icon-search',
            placeholder: '请输入'
          }
        ]
      },
      searchAction: [
        {
          label: '查询',
          type: 'primary',
          handle: () => {
            this.queryParams.currentPage = 1
            this.$refs.multipleTable.clearSelection()
          }
        },
        {
          label: '重置',
          type: 'primary',
          plain: true,
          handle: () => {
            this.queryString = {
              facilityNameCn: '',
              type: '',
              managementUnit: '',
              activeName: this.queryString.activeName
            }
            this.queryParams.currentPage = 1
          }
        }
      ]
    }
  },
  created() {
    this.getList()
  },
  mounted() { },
  methods: {
    addFacilitie() {
      this.$router.push({
        name: 'facilitiesInfo',
        params: { flag: 'add' }
      })
    },
    handleDownloadFile() {
      if (this.multipleSelection.length == 0) {
        this.$message({
          type: 'warning',
          message: '请选择要导出的数据'
        })
        return
      }
      const ids = []
      this.multipleSelection.forEach(items => {
        ids.push(items.id)
      })
      this.downloadFun(ids)
    },
    batchDelete() {
      if (this.multipleSelection.length == 0) {
        this.$message({
          type: 'warning',
          message: '请选择要删除的数据'
        })
        return
      }
      const ids = []
      this.multipleSelection.forEach(items => {
        ids.push(items.id)
      })
      this.deleteFun(ids)
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleClick() {
      this.queryParams.type = this.queryString.activeName
      this.getList()
    },
    handleUploadFile(file) {
      const _file = file?.raw ?? file
      const formData = new FormData()
      formData.append('file', _file)
      formData.append('pathType', file.name.split('.').pop())
      uploadMultiple(formData).then(() => {
        this.$message.success("操作成功！")
        this.getList()
      })
    },
    getList() {
      this.queryParams.report = 'examine'
      // console.log(this.queryParams,'this.queryParams');
      getFacilitiesList(this.queryParams).then(res => {
        this.tableData = res.data
        this.allData = this.tableData
        this.queryParams.total = res.total
      })
    },
    handleReport(row, status) {
      row.Report = status
      updateFacilities(row).then(() => {
        this.getList()
        this.$message.success("操作成功！")
      })
    },
    downloadFun(ids) {
      this.$confirm(`确定要导出吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          downloadMultiple(ids).then(() => {
            this.$message.success("操作成功！")
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    deleteFun(ids) {
      this.$confirm(`确定要删除吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteFacilitiesBatch(ids).then(() => {
            this.$refs.multipleTable.clearSelection()
            this.$message.success("操作成功！")
            this.getList()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    }
  }
}
</script>

<style lang="less" scoped></style>